#!/bin/bash

echo "Building Excel to PDF Converter..."

# 检查Maven
if ! command -v mvn &> /dev/null; then
    echo "Maven is not installed. Please install Maven first."
    exit 1
fi

# 检查Java
if ! command -v java &> /dev/null; then
    echo "Java is not installed. Please install Java 11+ first."
    exit 1
fi

echo "Java version:"
java -version

echo "Maven version:"
mvn --version

# 清理并打包
echo "Cleaning previous builds..."
mvn clean

echo "Packaging application..."
mvn package -DskipTests

# 检查构建是否成功
if [ $? -eq 0 ]; then
    echo "Build successful!"

    # 检查生成的JAR文件
    JAR_FILE=$(find target -name "*.jar" -not -name "*sources*" -not -name "*javadoc*" | head -1)

    if [ -n "$JAR_FILE" ]; then
        echo "JAR file: $JAR_FILE"

        # 查看MANIFEST
        echo "Checking MANIFEST.MF..."
        jar xf "$JAR_FILE" META-INF/MANIFEST.MF 2>/dev/null
        if [ -f "META-INF/MANIFEST.MF" ]; then
            cat META-INF/MANIFEST.MF
            rm -rf META-INF/
        fi

        # 创建符号链接方便使用
        ln -sf "$JAR_FILE" excel-to-pdf.jar
        echo "Created symlink: excel-to-pdf.jar -> $JAR_FILE"
    else
        echo "Error: No JAR file found!"
        exit 1
    fi
else
    echo "Build failed!"
    exit 1
fi